package com.ctsi.ssdc.controller.front;

import com.ctsi.ssdc.common.R;
import com.ctsi.ssdc.constants.SortedMapConstant;
import com.ctsi.ssdc.service.front.IndustrialIndicatorsService1;
import com.ctsi.ssdc.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.*;

@RestController
@RequestMapping("/industrialIndicators")
@CrossOrigin
public class IndustrialIndicatorsController1 {
    @Autowired
    IndustrialIndicatorsService1 industrialIndicatorsService1;



    /**
     * 分区镇规上工业总产值
     *
     * @param year(int) 年份, month(int) 月份
     *@return
     * List集合（SubDistrictTownIndustry类）
     *                {id
     *                 indicators
     *                 thisMonth
     *                 accumulate
     *                 monthOnMonth
     *                 ratio 规上占比
     *                 growthRanking 增速排名
     *                 statisticalTime
     *                 year
     *                 month}
     *
     */
    @GetMapping("/getSubDistrictTownIndustry")
    public R getSubDistrictTownIndustry(@RequestParam int year, @RequestParam int month) {
        List<SubDistrictTownIndustry> allInfoList = industrialIndicatorsService1.getSubDistrictTownIndustry(year, month);
        if(allInfoList == null || allInfoList.isEmpty()) return R.miss("不存在该信息");
        SubDistrictTownIndustry total =  null;
        SubDistrictTownIndustry zhou_zhuang = null; // 周庄
        SubDistrictTownIndustry dian_shan_hu = null; // 淀山湖
        SubDistrictTownIndustry jin_xi = null; // 锦溪
        List<SubDistrictTownIndustry> fiexedList = new ArrayList<>();
        List<SubDistrictTownIndustry> sortedList = new ArrayList<>();
        for (SubDistrictTownIndustry industry : allInfoList) {
            switch (industry.getIndicators()) {
                case "全市总计":
                    total = industry;
                    break;
                case "周庄镇":
                    zhou_zhuang = industry;
                    break;
                case "淀山湖镇":
                    dian_shan_hu = industry;
                    break;
                case "锦溪镇":
                    jin_xi = industry;
                    break;
                default:
                    fiexedList.add(industry);
                    sortedList.add(industry);
            }
        }
        fiexedList.sort(Comparator.comparingInt(a -> SortedMapConstant.get(a.getIndicators())));
        return R.ok().data("total", total).data("zhou_zhuang", zhou_zhuang)
                .data("dian_shan_hu", dian_shan_hu).data("jin_xi", jin_xi)
                .data("fiexedList", fiexedList).data("sortedList", sortedList);
    }


    /**
     * 工业总产值
     * 规上工业总产值
     * 综合能源消费量（吨标准煤）
     * 规上工业用电量（万千瓦时）
     * 万元产值能耗（吨标准煤/万元）
     *
     * @param year(int) 年份, indicators(String) 指标
     *@return
     * List集合（IndustrialEconomicIndicators类）
     *                {id
     *                 indicators
     *                 thisMonth
     *                 accumulate
     *                 monthOnMonth
     *                 statisticalTime
     *                 year
     *                 month}
     *
     */
    @GetMapping("/getIndustrialIndicatorsByIndicators")
    public List<IndustrialEconomicIndicators> getIndustrialIndicatorsByIndicators(@RequestParam int year, @RequestParam String indicators) {
        return industrialIndicatorsService1.getIndustrialIndicatorsByIndicators(year,indicators);
    }


    /**
     *高新技术产业产值
     *
     * @param year(int) 年份
     *@return
     * List集合（HighTechIndustry类）
     *                {id
     *                 thisMonth
     *                 accumulate
     *                 monthOnMonth
     *                 HighTechRatio 高新占比
     *                 statisticalTime
     *                 year
     *                 month}
     *
     */
    @GetMapping({"/getHighTechIndustry"})
    public List<HighTechIndustry> getHighTechIndustry(@RequestParam int year) {
        return industrialIndicatorsService1.getHighTechIndustry(year);
    }

    /**
     *头标：当月及当月累计
     *
     * @param year(int) 年份, month(int) 月份, indicators(String) 指标
     *@return
     * IndustrialEconomicIndicators类
     *                {id
     *                 indicators 指标
     *                 thisMonth 当月
     *                 accumulate 当月累计
     *                 monthOnMonth 同比
     *                 statisticalTime 日期
     *                 year 年
     *                 month 月}
     *
     */
    @GetMapping("/getMonthProduct")
    public IndustrialEconomicIndicators getMonthProduct(@RequestParam int year, @RequestParam int month, @RequestParam String indicators) {
        return industrialIndicatorsService1.getMonthProduct(year, month, indicators);
    }


    /**
     * 规模以上工业分行业产值
     * @param year 年份，month 月份
     *@return List<SectorProduct>
     *                {
     *                 number 序号 √
     *                 sectorName 分行业名称 √
     *                 currentMonth 本期当月 √
     *                 currentAccumulate 本期累计 √
     *                 previousAccumulate 同期累计
     *                 monthOnMothAccumulate 累计同比 √
     *                 }
     * √为前端需要的数据
     */
    @GetMapping("/getSectorProduct")
    public List<SectorProduct> getSectorProduct(@RequestParam int year, @RequestParam int month) {
        return industrialIndicatorsService1.getSectorProduct(year, month);
    }

}
